package com.dxj1992.controller.admin.steam;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dxj1992.controller.admin.BaseController;
import com.dxj1992.entity.SteamBalances;
import com.dxj1992.service.ISteamBalancesService;
import com.dxj1992.utils.CommonResult;
import com.dxj1992.utils.TableDataInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2022-04-08
 */
@Controller
@RequestMapping("/admin/steam/balance")
public class SteamBalancesController extends BaseController {

    private String prefix = "admin/steam/balance/";

    @Autowired
    private ISteamBalancesService steamBalancesService;

    @GetMapping("")
    public String index() {
        return prefix + "index";
    }

    @GetMapping("/add")
    public String add() {
        return prefix + "add";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SteamBalances steamBalances) {
        LambdaQueryWrapper<SteamBalances> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(steamBalances.getName())) {
            queryWrapper.like(SteamBalances::getName, steamBalances.getName());
        }
        List<SteamBalances> list = steamBalancesService.list(queryWrapper);
        for (SteamBalances balances : list) {
            double b = balances.getPrice() - balances.getCostPrice();
            balances.setProfit(Double.parseDouble(String.format("%.2f", b)));
            double v = Double.parseDouble(String.format("%.2f", b / balances.getPrice()));
            double v1 = v * 100;
            balances.setProfitRate(v1);
        }

        return getDataTable(list);
    }

    @PostMapping("/add")
    @ResponseBody
    public CommonResult addSave(SteamBalances steamBalances) {
        boolean save = steamBalancesService.save(steamBalances);
        if (!save) {
            return CommonResult.failed("保存失败");
        }
        return CommonResult.success("保存成功");
    }

    @PostMapping("/edit")
    @ResponseBody
    public CommonResult editSave(SteamBalances steamBalances) {
        SteamBalances model = steamBalancesService.getOne(Wrappers.<SteamBalances>lambdaQuery().eq(SteamBalances::getId, steamBalances.getId()));
        model.setName(steamBalances.getName());
        model.setUrl(steamBalances.getUrl());
        model.setCostPrice(steamBalances.getCostPrice());
        boolean b = steamBalancesService.updateById(model);
        if (!b) {
            return CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        SteamBalances steamBalances = steamBalancesService.getOne(Wrappers.<SteamBalances>lambdaQuery().eq(SteamBalances::getId, id));
        mmap.put("steamBalance", steamBalances);
        return prefix + "edit";
    }

    @PostMapping("/remove")
    @ResponseBody
    public CommonResult remove(@Param("ids") String ids) {
        Boolean b = steamBalancesService.removeData(ids);
        if (!b) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }

}
